<template>
	<div class="system-Brand-container layout-padding">
		<div class="system-Brand-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input v-model="state.search.brand_name" size="default" placeholder="请输入品牌名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="onSearch">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddBrand('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增品牌
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="id" label="ID" width="60" />
				<el-table-column prop="brand_name" label="品牌名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="brand_image" label="品牌LOGO" show-overflow-tooltip>
					<template #default="scope">
						<img :src="scope.row.brand_image" class="avatar" />
					</template>
				</el-table-column>
				<el-table-column prop="category_name" label="所属分类" show-overflow-tooltip></el-table-column>
				<el-table-column prop="sort" label="排序" show-overflow-tooltip></el-table-column>
				<el-table-column prop="is_show" label="状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.is_show">显示</el-tag>
						<el-tag type="info" v-else>隐藏</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="create_time" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button  size="small" text type="primary" @click="onOpenEditBrand('edit', scope.row)"
							>修改</el-button
						>
						<el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.search.page"
				background
				v-model:page-size="state.search.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</div>
		<BrandDialog ref="BrandDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="goodsBrand">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import  brandApi  from '/@/api/goods/brand/index.ts';

// 引入组件
const BrandDialog = defineAsyncComponent(() => import('./addEditDialog.vue'));

// 定义变量内容
const BrandDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
	},
	search: {
		brand_name: '',
		page: 1,
		pageSize: 20
	}
});

const getBrandData = async () => {
	state.tableData.loading = true;
	const res = await brandApi.listBrand(state.search);
	state.tableData.data = res.data.list;
	state.tableData.total = res.data.total;
	setTimeout(() => {
		state.tableData.loading = false;
	}, 500);
};

const onSearch = async () => {
	await getBrandData();
}



// 打开新增角色弹窗
const onOpenAddBrand = (type: string) => {
	BrandDialogRef.value.openDialog(type);
};
// 打开修改角色弹窗
const onOpenEditBrand = (type: string, row: Object) => {
	BrandDialogRef.value.openDialog(type, row);
};
// 删除角色
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除品牌：“${row.brand_name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
	.then(async () => {
		const res = await brandApi.deleteBrand({ids: row.id});
		if (res.code == 200) {
			getBrandData();
			ElMessage.success('删除成功');
		}
	})
	.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.search.pageSize = val;
	getBrandData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.search.pageSize = val;
	getBrandData();
};
// 页面加载时
onMounted(() => {
	getBrandData();
});
</script>

<style scoped lang="scss">
.system-Brand-container {
	.system-Brand-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
